// pages/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrls: [
            '../../images/tianqi/duoyun.png',
            '../../images/tianqi/qing.png',
            '../../images/tianqi/xiaoyu.png'
        ],
        cards: [

        ],
        indicatorDots: true, //是否显示面板指示点
        inputShowed: false,
        inputVal: "",
        touchS: [0, 0],
        touchE: [0, 0],
        name: "",
        intro: "",
        img: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.load(2)
        this.getFamous()
        this.getNews()
    },
    getNews() {
        let num = Math.floor(Math.random() * 3)
        wx.cloud.database().collection('news').skip(num).limit(2).get().then(res => {
            console.log(res.data)
            this.setData({
                news: res.data
            })
        })
    },
    load(n) {
        let num = Math.floor(Math.random() * 11)
        console.log(num)
        wx.cloud.database().collection('books').skip(num).limit(n).get().then(res => {
            this.setData({
                cards: this.data.cards.concat(res.data)
            })
        })
    },
    getFamous() {
        let num = Math.floor(Math.random() * 4)
        console.log(num)
        wx.cloud.database().collection('famous').skip(num).limit(1).get().then(res => {
            console.log(res.data)
            this.setData({
                name: res.data[0].name,
                intro: res.data[0].intro,
                img: res.data[0].img
            })
        })
    },
    touchStart: function (e) {
        // console.log(e.touches[0].pageX)
        let sx = e.touches[0].pageX
        let sy = e.touches[0].pageY
        this.data.touchS = [sx, sy]
    },
    touchMove: function (e) {
        let sx = e.touches[0].pageX;
        let sy = e.touches[0].pageY;
        this.data.touchE = [sx, sy]
    },
    touchEnd: function (e) {
        let start = this.data.touchS
        let end = this.data.touchE
        console.log(start)
        console.log(end)
        if (start[0] < end[0] - 50) {
            this.load(1)
        } else if (start[0] > end[0] + 50) {
            this.load(1)
        } else {
            console.log('静止')
        }
    },
    goDrama(e) {
        wx.navigateTo({
            url: '../details/details?title=' + e.currentTarget.dataset.name
        })
    },
    goNews(e) {
        wx.navigateTo({
            url: '../newscont/newscont?title=' + e.currentTarget.dataset.name
        })
    },
    goFamous(e) {
        wx.navigateTo({
            url: '../famouscont/famouscont?name=' + e.currentTarget.dataset.name
        })
    },
    goSearch() {
        wx.navigateTo({
            url: '../search/search',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})